package com.Rsoft.goodWebsocketMan.factory;

import com.Rsoft.goodWebsocketMan.handler.MyClientHandler;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.ExecutionException;
/*
* @author LR
* */
public class ClientFactory {
    public static WebSocketSession getClientSession(String url, Map<String, String> header) throws ExecutionException, InterruptedException, URISyntaxException {
        WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
        if (null != header && header.size() != 0){
            header.forEach(headers::add);
        }
        URI a = new  URI(url);
        System.out.println(URI.create(url).getHost());
        StandardWebSocketClient webSocketClient = new StandardWebSocketClient();
        ListenableFuture<WebSocketSession> listenableFuture =  webSocketClient.doHandshake(new MyClientHandler(),headers,a);
        return  listenableFuture.get();
    }
}
